package menu;

import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.hsqldb.lib.HashMap;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * menugui.java
 *
 * Created on 13-mar-2011, 21.36.50
 */

/**
 *
 * @author maurizio
 */
public class menugui extends javax.swing.JFrame {



    /** Creates new form menugui */
    public menugui() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = initJTree();
        jCheckBox1 = new javax.swing.JCheckBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTree3 = initJtree3();
        jButton7 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane7 = new javax.swing.JScrollPane();
        jList4 = new javax.swing.JList();
        jButtElIngr = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTreeModEl = initJTree();
        jButtEl = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton5 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane5 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jButton6 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel13 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jList2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jButton3.setText("Editor di testo");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton2.setText("Aggiungi");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Rimuovi");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jTree1);

        jCheckBox1.setText("Mostra ingredienti");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(jTree3);

        jButton7.setText("Reset");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel10.setText("Tenere premuto il tasto \"CTRL\" per selezionare più elementi");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(345, 345, 345)
                        .addComponent(jCheckBox1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 182, Short.MAX_VALUE)
                        .addComponent(jButton3))
                    .addComponent(jLabel10))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(211, 211, 211)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton7))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox1)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel10)
                .addGap(46, 46, 46))
        );

        jTabbedPane2.addTab("Menu", jPanel1);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Eliminazione ingredienti", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jList4.setModel(new javax.swing.AbstractListModel() {
            String[] strings = initJlist();
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane7.setViewportView(jList4);

        jButtElIngr.setText("Elimina");
        jButtElIngr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtElIngrActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                    .addComponent(jButtElIngr))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 518, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtElIngr))
        );

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("Eliminazione portate/piatti"));

        jScrollPane6.setViewportView(jTreeModEl);

        jButtEl.setText("Elimina");
        jButtEl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtElActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtEl))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 519, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtEl)
                .addGap(75, 75, 75))
        );

        jLabel6.setText("Tenere premuto il tasto ctrl per selezionare più elementi");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel6))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, 589, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Elimina", jPanel3);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Portata"));

        jLabel2.setText("Portata");

        jLabel4.setText("Posizione");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(initComboBox(2)));

        jButton5.setText("Aggiungi");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel8.setText("Inserimento nuova portata");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addContainerGap(202, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5)))
                .addGap(154, 154, 154))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jButton5)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Piatto"));

        jLabel1.setText("Portata");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(initComboBox(1)));

        jLabel5.setText("Piatto");

        jList3.setModel(new javax.swing.AbstractListModel() {
            String[] strings = initJlist();
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane5.setViewportView(jList3);

        jButton6.setText("Completa inserimento piatto");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel12.setText("Seleziona gli ingredienti relativi al piatto:");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12)
                .addContainerGap(68, Short.MAX_VALUE))
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(120, 120, 120)
                        .addComponent(jButton6)))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton6)
                .addContainerGap())
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Ingredienti"));

        jLabel7.setText("(Inserire gli ingredienti separati da virgola )");

        jButton4.setText("Aggiungi");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane4.setViewportView(jTextArea1);

        jLabel13.setText("Inserimento nuovi ingredienti");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel7)
                        .addComponent(jLabel13)
                        .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING)))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addContainerGap(3, Short.MAX_VALUE))
        );

        jLabel3.setText("Tenere premuto il tasto ctrl per selezionare più elementi");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE))
                        .addGap(18, 18, 18))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap(397, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel7, 0, 405, Short.MAX_VALUE))
                    .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, 596, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Aggiungi", jPanel2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 839, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 673, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jTabbedPane2.getAccessibleContext().setAccessibleName("Menù");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtElIngrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtElIngrActionPerformed
        Object ingredienti[];
        String ingrediente;

        /*  Connessione al database  */
        try {
            da = DataAccess.getInstance();
        } catch (SQLException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        ingredienti = jList4.getSelectedValues();
        if(ingredienti.length == 0)
            JOptionPane.showMessageDialog(null, "Nessun ingrediente selezionato","Attenzione", 1);
        else{
            for(int i=0;i < ingredienti.length; i++){
                ingrediente = ingredienti[i].toString();
                da.deleteIngrediente(ingrediente);
            }
        }
        refresh();
}//GEN-LAST:event_jButtElIngrActionPerformed

    private void jButtElActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtElActionPerformed
        String message, npiatto="", cod_portata ="";
        TreePath albero[];
        /*  Connessione al database  */
        try {
            da = DataAccess.getInstance();
        } catch (SQLException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }

        albero = jTreeModEl.getSelectionPaths();  // torna la lista dei nodi selezionati, null altrimenti
        if (albero == null)
            JOptionPane.showMessageDialog(null, "Nessun elemento selezionato","Attenzione", 1);
        else {
            if(JOptionPane.showConfirmDialog(null, "Sei sicuro di voler definitivamente eliminare gli elementi selezionati?", "Conferma eliminazione", JOptionPane.YES_NO_OPTION) == 0){

                message = "Hai cancellato i seguenti elementi: \n";
                for(int i=0;i < albero.length; i++){

                    switch (albero[i].getPathCount()){

                        case 1:
                            JOptionPane.showMessageDialog(null, "Non è possibile eliminare l'intero menù","Attenzione", 1);
                            break;

                        case 2: // eliminazione portata
                            if(JOptionPane.showConfirmDialog(null, "Stai per eliminare la portata "+albero[i].getLastPathComponent().toString().substring(5)+ " e TUTTI i piatti ad essa associati, vuoi comunque procedere?", "Conferma eliminazione", JOptionPane.YES_NO_OPTION) == 0){
                                // ELIMINAZIONE
                                cod_portata = albero[i].getLastPathComponent().toString().substring(5);
                                cod_portata = cod_portata.toLowerCase();
                                if(da.deletePortata(cod_portata) != 0);
                                message= message + albero[i].getLastPathComponent().toString()+"\n";
                            }
                            break;

                        case 3: // eliminazione piatto
                            npiatto = albero[i].getLastPathComponent().toString();
                            if(da.deletePiatto(npiatto) != 0);
                            message= message + albero[i].getLastPathComponent().toString()+"\n";
                            break;
                    }
                }
                //JOptionPane.showMessageDialog(null, message,"Elementi cancellati", 1);
                refresh();
            }
        }
}//GEN-LAST:event_jButtElActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        //elimina l'albero di selezione
        resetJtree3();
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        showingredients();
}//GEN-LAST:event_jCheckBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //rimuovi elemento da albero di selezione
        remfromSelMenu();
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        //crea albero selezione per il menù
        fillTreeSelMenu();
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {
            new odf(jTree3);
        } catch (Exception ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        //inserimento portata
        try {
            da = DataAccess.getInstance();
        } catch (SQLException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        int ret=da.addPortata(jTextField1.getText(),jComboBox2.getSelectedIndex()+1);

        switch (ret){
            case 0:
                showDialogue(1,"Esiste già una portata con lo stesso nome");
                break;
            case 1:
                showDialogue(0,"Portata inserita correttamente");
                break;
            case 2:
                showDialogue(1,"Riempire il campo portata per effettuare l'inserimento");
                break;
        }
        refresh();
}//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        //inserimento piatto
        int ret=0;
        try {
            da = DataAccess.getInstance();
        } catch (SQLException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            ret = da.addPiatto(jTextField2.getText().toString(), jComboBox1.getSelectedItem().toString(), jList3.getSelectedValues());
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        switch (ret){
            case 0:
                showDialogue(1,"Esiste già un piatto con lo stesso nome");
                break;
            case 1:
                showDialogue(0,"Piatto inserito correttamente");
                break;
            case 2:
                showDialogue(1,"Riempire i campi portata e piatto per effettuare l'inserimento");
                break;
        }
        refresh();
}//GEN-LAST:event_jButton6ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

        //inserimento ingrediente
        int ret=0;
        try {
            da = DataAccess.getInstance();
        } catch (SQLException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            ret = da.addIngredienti(jTextArea1.getText());
        } catch (SQLException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        switch (ret){
            case 0:
                showDialogue(1,"Esiste già un ingrediente con lo stesso nome");
                break;
            /*case 1:
                showDialogue(0,"Ingrediente/i inserito correttamente");
                break;*/
            case 2:
                showDialogue(1,"Specificare nell'area testo gli ingredienti da inserire");
                break;
        }
        refresh();
}//GEN-LAST:event_jButton4ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtEl;
    private javax.swing.JButton jButtElIngr;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JList jList2;
    private javax.swing.JList jList3;
    private javax.swing.JList jList4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTree jTree1;
    private javax.swing.JTree jTree3;
    private javax.swing.JTree jTreeModEl;
    // End of variables declaration//GEN-END:variables
    private DataAccess da;


    private void showingredients(){
        DefaultMutableTreeNode newroot =new DefaultMutableTreeNode("Menù del giorno");
        if (jCheckBox1.isSelected()){
            try {
                int i = 0;
                int j = 0;
                DefaultMutableTreeNode ingredientinode = new DefaultMutableTreeNode();
                String ingredienti = null;
                //System.out.println("checked");
                da = DataAccess.getInstance();
                DefaultMutableTreeNode piatto = null, portata=null;
                DefaultMutableTreeNode root3 = (DefaultMutableTreeNode) jTree3.getModel().getRoot();
                int Npiatti = 0;
                int NPort3 = jTree3.getModel().getChildCount(root3);
                if (NPort3 != 0) {
                    for (i = 0; i < NPort3; i++) {
                        portata= new DefaultMutableTreeNode(root3.getChildAt(i).toString());
                        Npiatti = (root3.getChildAt(i)).getChildCount();
                        for (j = 0; j < Npiatti; j++) {
                            ingredienti = arrayToString((da.getIngredienti(root3.getChildAt(i).getChildAt(j).toString())), ", ");
                            //System.out.println(ingredienti);
                            piatto = new DefaultMutableTreeNode(root3.getChildAt(i).getChildAt(j).toString());
                            if (!(ingredienti.isEmpty())){
                                ingredientinode = new DefaultMutableTreeNode(ingredienti);
                                piatto.add(ingredientinode);
                            }
                            portata.add(piatto);
                        }
                        newroot.add(portata);
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            //System.out.println("unchecked");
                int i = 0;
                int j = 0;
                DefaultMutableTreeNode piatto = null, portata=null;
                DefaultMutableTreeNode root3 = (DefaultMutableTreeNode) jTree3.getModel().getRoot();
                int Npiatti = 0;
                int NPort3 = jTree3.getModel().getChildCount(root3);
                            if (NPort3 != 0) {
                    for (i = 0; i < NPort3; i++) {
                        portata= new DefaultMutableTreeNode(root3.getChildAt(i).toString());
                        Npiatti = (root3.getChildAt(i)).getChildCount();
                        for (j = 0; j < Npiatti; j++) {
                            //System.out.println(ingredienti);
                            piatto = new DefaultMutableTreeNode(root3.getChildAt(i).getChildAt(j).toString());
                            piatto.removeAllChildren();
                            portata.add(piatto);
                        }
                        newroot.add(portata);
                    }
                }

        }
        jTree3=new JTree(newroot);
        refresh();
    }


    private void remfromSelMenu(){
        if ((jTree3.getModel().getChildCount(jTree3.getModel().getRoot()))!=0){
            TreePath[] ris = jTree3.getSelectionPaths();
            DefaultTreeModel tmodel = (DefaultTreeModel) jTree3.getModel();
            int i=0;
            if (!(ris==null)){
                for (i=0; i<ris.length; i++){
                tmodel.removeNodeFromParent((MutableTreeNode) (ris[i].getLastPathComponent()));
                }
            }
        refresh();
        }
    }

    private void fillTreeSelMenu(){
        TreePath[] ris = jTree1.getSelectionPaths();
        DefaultMutableTreeNode resroot = (DefaultMutableTreeNode) jTree3.getModel().getRoot();
        DefaultMutableTreeNode mergeroot = new DefaultMutableTreeNode("Menù del giorno");
        DefaultMutableTreeNode root = new DefaultMutableTreeNode();
        DefaultMutableTreeNode newroot =new DefaultMutableTreeNode("Menù del giorno");
        DefaultMutableTreeNode newchildp,newchild;
        root=(DefaultMutableTreeNode)jTree1.getModel().getRoot();
        int NPort=(jTree1.getModel().getChildCount(root));
        String portata=null, portatasel=null;
        int i=0,j=0,z=0,q=0,s=0;
            if (!(ris==null)){//Selezione non nulla
                for (i=0; i<NPort; i++){
                    portata=(root.getChildAt(i)).toString();
                    //System.out.println(portata);
                    newchildp = new DefaultMutableTreeNode(portata);//nuovo nodo portata
                    for (j=0; j<ris.length; j++){
                      //System.out.println("ciaociao");
                        if (ris[j].getPathCount()==3){
                            portatasel=((ris[j].getParentPath()).getLastPathComponent()).toString();
                            if (portatasel.equals(portata)){
                                    newchild=new DefaultMutableTreeNode(ris[j].getLastPathComponent().toString());//nuovo nodo piatto
                                    newchildp.add(newchild);
                                }
                            }
                        }
                    if (!newchildp.isLeaf()){
                        newroot.add(newchildp);
                    }
                }
                resroot=newroot;
                DefaultMutableTreeNode root3 =(DefaultMutableTreeNode)jTree3.getModel().getRoot();
                int NPort3=(jTree3.getModel().getChildCount(root3));//numero di portate su albero a dx
                //System.out.println(NPort3+" nport3");
                if (NPort3!=0){//è già stato fatto un inserimento nell'albero a dx, fondo i due alberi.
                    DefaultMutableTreeNode piattoold = new DefaultMutableTreeNode();
                    DefaultMutableTreeNode piattonew = new DefaultMutableTreeNode();
                    JTree newr = new JTree(newroot);
                    DefaultTreeModel modelnewr = (DefaultTreeModel) newr.getModel();
                    int NPortnew=(newr.getModel().getChildCount(newroot));//numero di portate selezionate
                    int numpiattiold,numpiattinew;
                    String portataold=null, portatanew=null,portatamerge=null;
                    for (i=0; i<NPort;i++){//Itero per ogni portata
                        portata=(root.getChildAt(i)).toString();
                        newchildp = new DefaultMutableTreeNode(portata);//nuovo nodo portata
                        for (j=0;j<NPort3;j++){//Albero a dx, trasferisco il contenuto su mergeroot
                            portataold=(root3.getChildAt(j)).toString();
                            if(portataold.equals(portata)){//per le portate uguali itero trasferendo tutti i piatti.
                                numpiattiold=(root3.getChildAt(j)).getChildCount();
                                //System.out.println(numpiattiold+"old");
                                for (z=0;z<numpiattiold; z++){
                                    piattoold= new DefaultMutableTreeNode((root3.getChildAt(j)).getChildAt(z).toString());
                                    newchildp.add(piattoold);
                                    //ora devo verifcare se il piatto è già presente nell'albero newroot creato nella selezione
                                    for (q=0;q<NPortnew;q++){//itero per le portate sull'albero scaturito dalla selezione
                                        portatanew=(newroot.getChildAt(q)).toString();
                                        if (portatanew.equals(portataold)){
                                            numpiattinew=(newroot.getChildAt(q)).getChildCount();
                                            //System.out.println(numpiattinew+"new");
                                                for (s=0;s<numpiattinew;s++){
                                                    piattonew=new DefaultMutableTreeNode((newroot.getChildAt(q)).getChildAt(s).toString());
                                                    if (((piattonew.toString()).equals(piattoold.toString()))){
                                                        //System.out.println(piattonew.toString()+" "+piattoold.toString());
                                                        modelnewr.removeNodeFromParent((MutableTreeNode) (newroot.getChildAt(q)).getChildAt(s));
                                                        numpiattinew=(newroot.getChildAt(q)).getChildCount();
                                                    }
                                                }
                                            if (newroot.getChildAt(q).isLeaf()){
                                                modelnewr.removeNodeFromParent((MutableTreeNode) (newroot.getChildAt(q)));//rimuovo portata nell'albero generato dalla selezione, poiché i relativi piatti, insieme ai corrispondenti presenti nell'albero di dx, sono stati trasferiti su mergenode
                                            }
                                        }
                                        NPortnew=((new JTree(newroot)).getModel().getChildCount(newroot));
                                    }
                                }

                            }
                        }
                        NPortnew=((new JTree(newroot)).getModel().getChildCount(newroot));
                        portata=(root.getChildAt(i)).toString();
                        for (q=0; q<NPortnew; q++){
                            portatanew=(((newroot.getChildAt(q)).toString()));
                            if (portatanew.equals(portata)){
                                numpiattinew=(newroot.getChildAt(q)).getChildCount();
                                //System.out.println(numpiattinew+"new");
                                    for (s=0;s<numpiattinew;s++){
                                        piattonew=new DefaultMutableTreeNode((newroot.getChildAt(q)).getChildAt(s).toString());
                                        newchildp.add(piattonew);
                                    }
                            }
                        }
                        if (!newchildp.isLeaf()){
                            mergeroot.add(newchildp);
                            }
                    }
                    resroot=mergeroot;
                }
            }
        jTree3=new JTree(resroot);
        if (jCheckBox1.isSelected()){
            showingredients();
        }
        //System.out.println((jTree3.getModel().getChildCount(newroot)));
        refresh();

}

    private JTree initJtree3(){
        DefaultMutableTreeNode root;
        JTree tree3=new JTree();
        if (jTree3==null){
            root=new DefaultMutableTreeNode("Menù del giorno");
            tree3 = new javax.swing.JTree(root);
        }else{
            tree3=jTree3;
        }
        ImageIcon Icon = createImageIcon("menu/node2.gif");
            if (Icon != null) {
                DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
                renderer.setOpenIcon(Icon);
                renderer.setClosedIcon(Icon);
                renderer.setLeafIcon(Icon);
                tree3.setCellRenderer(renderer);
            }
        for (int i = 0; i < tree3.getRowCount(); i++) {
            tree3.expandRow(i);
        }

        return tree3;
    }

    private void resetJtree3(){
        jTree3=null;
        jCheckBox1.setSelected(false);
        refresh();
    }

    private JTree initJTree (){
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("MenuDB");
        try {
            da = DataAccess.getInstance();
            HashMap dati = da.tree();

            org.hsqldb.lib.Set list  =  dati.keySet();
            org.hsqldb.lib.Iterator iter = list.iterator();
            int i=0;
            String temp=null;
            DefaultMutableTreeNode PortataNode;
            DefaultMutableTreeNode PiattoNode;
            String portata;
            ArrayList piatti;
            String piatto;
            while(iter.hasNext()) {
                portata = iter.next().toString();
                temp=(++i)+"° - "+(portata).toUpperCase();
                PortataNode=new DefaultMutableTreeNode(temp);                
                root.add(PortataNode);
                piatti = (ArrayList) dati.get(portata);
                Iterator iter2 = piatti.iterator();
                while (iter2.hasNext()){
                    piatto = iter2.next().toString();
                    PiattoNode=new DefaultMutableTreeNode(piatto);
                    PortataNode.add(PiattoNode);
                }
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        JTree settedJT = new javax.swing.JTree(root);
        settedJT.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
        ImageIcon Icon = createImageIcon("menu/node2.gif");
        if (Icon != null) {
            DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
            renderer.setOpenIcon(Icon);
            renderer.setClosedIcon(Icon);
            renderer.setLeafIcon(Icon);
            settedJT.setCellRenderer(renderer);
        }
        for (int i = 0; i < settedJT.getRowCount(); i++) {
            settedJT.expandRow(i);
        }
        return settedJT;
    }

 /* private static TreeSelectionListener Tlis = new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent se) {
            JTree tree = (JTree) se.getSource();
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if(!selectedNode.isLeaf() || (selectedNode.isRoot()) || (selectedNode.getDepth()<=1)){
                
            }
            
      }
  };*/

    private Object[] initComboBox (int sel) {
        Object[] array =null;
        Integer[] arrayI=null;
        String[] arrayS=null;
        try {
            da = DataAccess.getInstance();
        } catch (SQLException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        switch (sel) {
            case 1://Jcombobox1 relativa alle portate
                try {
                    arrayS = (String[]) da.getPortata();
                } catch (SQLException ex) {
                    Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (arrayS.length==0){
                    String[] tempS={"0 portate"};
                    arrayS=tempS;
                    }
                array=arrayS;
                break;
            case 2://Jcombobox2 relativa alla posizione della portata da inserire
                try { 
                    arrayI = (Integer[]) da.getPos();
                } catch (SQLException ex) { Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex); }
                if (arrayI.length<=1){
                        Integer[] temp={1};
                        arrayI=temp;
                        //System.arraycopy(temp,0,array,0,0);
                    }else{
                        arrayI[arrayI.length-1]=arrayI[arrayI.length-2]+1;
                    }
                    array=arrayI;

                break;

            default:
                break;
        }
        return array;
    }

    private String[] initJlist(){
        String[] array=null;
        try {
            da = DataAccess.getInstance();
            array = da.getIngredienti(null);
        } catch (SQLException ex) {
            Logger.getLogger(menugui.class.getName()).log(Level.SEVERE, null, ex);
        }
        if ((array.length==0)){
            array = new String[1];
            array[0]="Gli ingredienti non sono ancora stati inseriti";
        }
        return array;
    }

    private ImageIcon createImageIcon(String path) {
        ClassLoader cldr = this.getClass().getClassLoader();
        java.net.URL imgURL  = cldr.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    private void refresh(){
        jTree1=initJTree();
        jTree3=initJtree3();
        jScrollPane1.setViewportView(jTree1);
        jScrollPane2.setViewportView(jTree3);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(initComboBox(1)));
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(initComboBox(2)));
        jList3.setModel(new javax.swing.AbstractListModel() {
            String[] strings = initJlist();
            @Override
            public int getSize() { return strings.length; }
            @Override
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList4.setModel(new javax.swing.AbstractListModel() {
            String[] strings = initJlist();
            @Override
            public int getSize() { return strings.length; }
            @Override
            public Object getElementAt(int i) { return strings[i]; }
        });
        jTreeModEl=initJTree();
        jScrollPane6.setViewportView(jTreeModEl);
    }

    private void showDialogue (int i, String message){
        switch (i){
            case 0://simple message (notification)
                JOptionPane.showMessageDialog(null, message, "Notifica", 1);
                break;
            case 1: //simple message (attention)
                JOptionPane.showMessageDialog(null, message, "Attenzione", 2);
                break;
            case 2: //yes/no/cancel message
                JOptionPane.showConfirmDialog(null, message);
                break;
        }
    }

    public static String arrayToString(String[] a, String separator) {
        String result = "";
        if (a.length > 0) {
            result = a[0];    // start with the first element
            for (int i=1; i<a.length; i++) {
                result = result + separator + a[i];
            }
        }
        return result;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new menugui().setVisible(true);
            }
        });
    }
}